package com.ltmonitor.web.controller.command;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.web.util.JsonMessage;

@Controller
@RequestMapping("/phoneBook")
public class PhoneBookAction extends TerminalCommandAction {
	
	@RequestMapping("/view.action")
	public String view(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/phoneBook";
	}

	/**
	 * 
	 * @param configType 信息项目设置类型, 0：删除终端全部信息项； 1：更新菜单； 2：追加菜单； 3：修改菜单
	 * @param tagIds
	 * @param phoneNumbers
	 * @param contacts
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage execute(int vehicleId,int configType, int[] tagIds,
			String[] phoneNumbers, String[] contacts) {

		try {
			StringBuilder sb = new StringBuilder();
			for (int m = 0; m < tagIds.length; m++) {
				sb.append(tagIds[m]).append(",").append(phoneNumbers[m])
						.append(",").append(contacts[m]).append(";");
			}

			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_PHONE_BOOK);
			tc.setCmdData(sb.toString());
			tc.setCmd("" + configType);
			tc.setVehicleId(vehicleId);
			SendCommand(tc);

			return json(true, tc.getEntityId());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(true, e.getMessage());
		}
	}

}
